const config = {
	dbName:"uu",
	dbPath: "_doc/uu.db",
	tables:{
		uu_book:"uu_book",
		uu_bookCategory:"uu_bookCategory",
		uu_bookChapter:"uu_bookChapter",
		uu_chapterContent:"uu_chapterContent",
		uu_bookShelf:"uu_bookShelf",
		uu_setting:"uu_setting"
	}
}

//我这个封装通过promise返回出去！！！
//创建数据库或者有该数据库就打开,    这一步必须要！
function openSqlite(){
	//创建数据库或者打开
	//这plus.sqlite只在手机上运行
	return new Promise((resolve,reject) =>{
		plus.sqlite.openDatabase({
			name:'uu',  //数据库名称
			path:'_doc/uu.db',   //数据库地址，uniapp推荐以下划线为开头，这到底存在哪里去了，我也不清楚，哈哈
			success(e){
				resolve(e); //成功回调
			},
			fail(e){
				reject(e); //失败回调
			}
		})
	})	
}

//在该数据库里创建表格，   这一步也必须要！
//下面注释里说的都是说sql:'create table if not exists....这里
//userInfo是表格名，你也可以写其他的名，不能用数字作为表格名的开头！！！
//括号里是表格的结构，列，这里我写了四列，list,id,gender,avatar这四列
//list后面大写的英文是自动增加的意思，因为表格里的每一行必须有唯一标识
//这sql语句会数据库的应该都看的懂，我是前端菜鸡，所以详细说明以便跟我一样不懂sql的前端看
//"id" TEXT  意思是这一列放的值为字符串之类的，如果是想存数字之类的就改为INTEGER
//数据库不能存对象，数组
function createTable(tableName,data){
	return new Promise((resolve,reject) =>{
	//创建表格在executeSql方法里写
		plus.sqlite.executeSql({
			name:'uu',
			//表格创建或者打开，后面为表格结构
			sql:`create table if not exists ${tableName}(${data})`, 
			success(e){
				resolve(e);
			},
			fail(e){
				reject(e);
			}
		})
	})
}

// 向表里面添加一条数据
function insert(tableName ,obj){
	if(tableName == null){
		return new Promise((resolve,reject)=>{reject("错误添加,表名为null")})
	}
	var b = (JSON.stringify(obj) == "{}");
	if(obj==null || b=="{}"){
		return new Promise((resolve,reject)=>{reject("错误添加,数据不能为{}")})
	}
	let keys = Object.keys(obj)
	let values = Object.values(obj)
	if(keys.length == 0 || values.length ==0 ){
		return new Promise((resolve,reject)=>{reject("错误添加，列名或数据不能为{}")})
	}
	let colums = keys.join(",")
	let rows = "'"+values.join("','")+"'"
	return new Promise((resolve,reject)=>{
		plus.sqlite.executeSql({
			name:"uu",
			sql:`insert into ${tableName}(${colums}) values(${rows})`,
			success: (e) => {
				resolve(e)
			},
			fail: (e) => {
				reject(e)
			}
		})
	})
}

//删除书架
function removeBookShelf(values){
	let ids = "'"+values.join("','")+"'"
	return new Promise((resolve,reject)=>{
		plus.sqlite.executeSql({
			name:"uu",
			sql:`delete from uu_bookShelf where fictionId in (${ids})`,
			success: (e) => {
				resolve(e)
			},
			fail: (e) => {
				reject(e)
			}
		})
	})
}

//关闭数据库
function closeSQL(name){
	return new Promise((resolve,reject) =>{
		plus.sqlite.closeDatabase({
			name:'uu',
			success(e){
				resolve(e);
			},
			fail(e){
				reject(e);
			}
		})
	})
}

//监听数据库是否开启
function isOpen(name,path){
	var ss = name || 'uu';
	var qq = path || '_doc/uu.db';
	//数据库打开了就返回true,否则返回false
	var open = plus.sqlite.isOpenDatabase({
					name:ss,
					path:qq
				})
	return open;
}


// 查所有小说
function selectBookAll(tableName,fictionId){
	let sqlstr = `select * from ${tableName} `
	if(fictionId){
		sqlstr = `select * from ${tableName} where fictionId = '${fictionId}'`
	}
	return new Promise((resolve,reject) =>{
		plus.sqlite.selectSql({
			name:'uu',
			sql:sqlstr,
			success(e){
				resolve(e);
			},
			fail(e){
				reject(e);
			}
		})
	})
}
//查询某张表 / 某个字段
function selectContent(tableName,data){
	if(data==null || JSON.stringify(data)=="{}"){
		return new Promise((resolve,reject)=>{reject("错误查询")})
	}
	let columns = Object.keys(data) 
	let values = Object.values(data)
	if(columns.length == 0 || values.length ==0 ){
		return new Promise((resolve,reject)=>{reject("错误查询")})
	}
	let column = columns[0]
	let value = values[0]
	let sqlstr = `select * from ${tableName} where ${column} = '${value}'`
	return new Promise((resolve,reject) =>{
		plus.sqlite.selectSql({
			name:'uu',
			sql:sqlstr,
			success(e){
				resolve(e);
			},
			fail(e){
				reject(e);
			}
		})
	})
}
//查询
function find(tableName,data){
	let sqlstr = ""
	if(data==null || JSON.stringify(data)=="{}"){
		sqlstr =  `select * from ${tableName}`;
	}else{
		let columns = Object.keys(data)
		let values = Object.values(data)
		let column = columns[0]
		let value = values[0]
		sqlstr = `select * from ${tableName} where ${column} = '${value}'`
	}
	return new Promise((resolve,reject) =>{
		plus.sqlite.selectSql({
			name:'uu',
			sql:sqlstr,
			success(e){
				resolve(e);
			},
			fail(e){
				reject(e);
			}
		})
	})
}

//查询总数
function findCount(tableName,data){
	let sqlstr = ""
	if(data==null || JSON.stringify(data)=="{}"){
		sqlstr =  `select count(1) from ${tableName}`;
	}else{
		let columns = Object.keys(data)
		let values = Object.values(data)
		let column = columns[0]
		let value = values[0]
		sqlstr = `select count(1) count from ${tableName} where ${column} = '${value}'`
	}
	return new Promise((resolve,reject)=>{
		plus.sqlite.selectSql({
				name:'uu',
				sql:sqlstr,
				success(e){
					return resolve(e)
				},
				fail(e){
					return reject(e)
				}
			})
	}) 
}

//删除
function remove(tableName,data){
	let sqlstr = ""
	if(data==null || JSON.stringify(data)=="{}"){
		sqlstr =  `delete from ${tableName}`;
	}else{
		let columns = Object.keys(data)
		let values = Object.values(data)
		let column = columns[0]
		let value = values[0]
		sqlstr = `delete from ${tableName} where ${column} = '${value}'`
	}
	return new Promise((resolve,reject) =>{
		plus.sqlite.selectSql({
			name:'uu',
			sql:sqlstr,
			success(e){
				resolve(e);
			},
			fail(e){
				reject(e);
			}
		})
	})
}
//修改
function update(tableName,data,condtion){
	if(data==null || JSON.stringify(data)=="{}"){
		return new Promise((resolve,reject)=>{reject("错误操作")})
	}
	
	let columns = Object.keys(data)
	let values = Object.values(data)
	let	sqlstr = `update ${tableName} set `
	for(let i =0;i< columns.length;i++){
		sqlstr = sqlstr + columns[i] +" = '"+ values[i]+"'"
		if(i<columns.length-1){
			sqlstr= sqlstr +","
		}
	}
	sqlstr = sqlstr + ` where ${condtion}`
	return new Promise((resolve,reject) =>{
		plus.sqlite.selectSql({
			name:'uu',
			sql:sqlstr,
			success(e){
				resolve(e);
			},
			fail(e){
				reject(e);
			}
		})
	})
}
//把这些方法导出去
export{
	config,
	openSqlite,
	createTable,
	insert,
	selectBookAll,
	isOpen,
	removeBookShelf,
	selectContent,
	find,
	remove,
	update,
	findCount
}